<template>
  <div>
    <h3 class="text-primary">政策法规</h3>
    <table class="table col-lg-8">
      <thead>
        <tr>
          <th scope="col"></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="index in range(start, end)" :key="index">
          <td>
            <router-link
              :to="{
                path: '/governmentZcfgNewsPage',
                query: {
                  title: title[index],
                  source: source[index],
                  date: date[index],
                  url: url[index],
                },
              }"
              class="btn"
              >{{ title[index] }}</router-link
            >
          </td>
        </tr>
      </tbody>
    </table>
    <div class="row">
      <div class="col-lg-2"></div>
      <div class="col-lg-3">
        <h5>
          <button class="badge bg-secondary" v-on:click="pageReduce()">
            上一页
          </button>
        </h5>
      </div>
      <div class="col-lg-2"></div>
      <div class="col-lg-3">
        <h5>
          <button class="badge bg-secondary" v-on:click="pageAdd()">
            下一页
          </button>
        </h5>
      </div>
      <div class="col-lg-2"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: [],
      source: [],
      date: [],
      url: [],
      start: 0,
      end: 0,
      begin: 0,
      part: 0,
      pageNum: 8,
      dataLength: 0,
      showNews: false,
      showNoData: true,
    };
  },
  methods: {
    range(start, end) {
      var arr = [];
      for (var i = start; i < end; i++) {
        arr.push(i);
      }
      return arr;
    },
    // 点击下一页进行翻页
    pageAdd() {
      var gap = this.dataLength - this.end;
      if (gap >= this.pageNum) {
        this.start += this.pageNum;
        this.end += this.pageNum;
      } else if (this.end != this.dataLength) {
        this.start += this.pageNum;
        this.end = this.dataLength;
      }
    },
    // 点击上一页进行翻页
    pageReduce() {
      if (this.end == this.dataLength) {
        this.start -= this.pageNum;
        this.end = this.part * this.pageNum;
      } else if (this.start != 0) {
        this.start -= this.pageNum;
        this.end -= this.pageNum;
      }
    },
  },
  // 获取所有新闻数据
  mounted() {
    this.axios
      .get(
        "http://122.51.157.58:8081/api-service/getZcfgInfo?key=123456"
      )
      .then((body) => {
        var result = body.data;
        this.dataLength = result.data.result.length;
        for (var i = 0; i < result.data.result.length; i++) {
          this.title.push(result.data.result[i].title);
          this.source.push(result.data.result[i].publishsector);
          this.date.push(result.data.result[i].publishtime);
          this.url.push(result.data.result[i].link);
        }
        this.part = Math.floor(result.data.result.length / this.pageNum);
        if (this.part == 0) {
          this.end = this.data.length;
        } else {
          this.end = this.pageNum;
        }
      });
  },
};
</script>